<?php
/**
 * Holds class Session
 *
 * @author daantimmer
 *
 * @package Library
 */

/**
 * Session wrapper class
 *
 * Additional functions added to the $_SESSION variable. This is just a wrapper around it.
 * All session-usage should go through this class.
 * Any modifications of the $_SESSION itself will not be reflected on the next request!
 *
 * @author daantimmer
 */
class Session{
	
	private $vars;
	
	private static $instance = null;
	
	private function __construct(){
		$this->create();
	}
	
	public function __destruct(){
		$_SESSION = $this->vars;
		session_write_close();
	}
	
	public function __get($var){
		return isset($this->vars[$var]) ? $this->vars[$var] : null;
	}
	
	public function __set($var,$val){
		$this->vars[$var] = $val;
	}
	
	public function __isset($var){
		return isset($this->vars[$var]);
	}
	
	/**
	 * Singleton instance of Session
	 *
	 * @return Session
	 */
	public static function instance(){
		if(self::$instance === null)
			self::$instance = new Session();
		return self::$instance;
	}
	
	public function create(){
		@session_start();
		$this->vars = $_SESSION;
	}
	
	public function destroy(){
		$this->vars = array();
		session_destroy();
	}
	
	public function regenerate(){
		$this->vars = array();
		session_regenerate_id(true);
	}
}
